/*
 * Copyright (C) 2017 Baidu, Inc. All Rights Reserved.
 */
package com.jinyi.idcardcamera.weight;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.IntDef;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.core.content.res.ResourcesCompat;

import com.jinyi.idcardcamera.R;
import com.jinyi.idcardcamera.utils.DimensionUtil;

import java.io.File;

@SuppressWarnings("unused")
public class MaskFaceView extends View {

    private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private int maskColor = Color.parseColor("#353535");
    private Rect frameRect = new Rect();

    public MaskFaceView(Context context) {
        super(context);
        init();
    }

    public MaskFaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MaskFaceView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(maskColor);
    }

    public Rect getFrameRect() {
        return new Rect(frameRect);
    }

    public void setMaskColor(int maskColor) {
        this.maskColor = maskColor;
        this.mPaint.setColor(maskColor);
    }

    @Override
    protected void onDraw(@NonNull Canvas canvas) {
        super.onDraw(canvas);
        drawCircleMask(canvas);
    }

    /**
     * 绘制圆圈遮罩
     * @param canvas
     */
    private void drawCircleMask(Canvas canvas) {
        int frameLeft = (getWidth() / 2) - DimensionUtil.dpToPx(131);
        int frameTop = DimensionUtil.dpToPx(176) - DimensionUtil.dpToPx(131);
        int frameRight = (getWidth() / 2) + DimensionUtil.dpToPx(131);
        int frameBottom = DimensionUtil.dpToPx(176) + (DimensionUtil.dpToPx(131) * 2);
        frameRect = new Rect(frameLeft,frameTop,frameRight,frameBottom);
        canvas.save();
        //目标图Dst
        canvas.drawRect(new Rect(0,0,getWidth(),getBottom()), mPaint);
        //设置混合模式
        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
        //源图Src，重叠区域右下角部分
        //cx圆心X坐标  cy圆心Y坐标  radius半径
        canvas.drawCircle((float) getWidth() / 2, DimensionUtil.dpToPx(176), DimensionUtil.dpToPx(131), mPaint);
        //清除混合模式
        mPaint.setXfermode(null);
        canvas.restore();
    }
}
